package top.kaoshanji.p4ch05a;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import top.kaoshanji.p4ch05a.mapper.UserMapper;
import top.kaoshanji.p4ch05a.model.User;
import top.kaoshanji.p4ch05a.util.SqlSessionUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/20 13:50
 */
public class UserMapperTest {

    /**
     * 代码清单test-p4ch05a-113页a：动态SQL语句if标签
     */
    @Test
    public void testQueryUserWithIf() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            List<User> userList = userMapper.queryUserWithIf("lucy", "");
            System.out.println(userList);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p4ch05a-115页a：动态SQL语句where标签
     */
    @Test
    public void testQueryUserWithWhere() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            List<User> userList = userMapper.queryUserWithWhere("lucy", "");
            System.out.println(userList);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p4ch05a-117页a：动态SQL语句Choose标签
     */
    @Test
    public void testQueryUserWithChoose() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            List<User> userList = userMapper.queryUserWithChoose("", "123456");
            System.out.println(userList);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p4ch05a-119页a：动态SQL语句Set标签
     */
    @Test
    public void testUpdateUserWithSet() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            User user = new User(1, "tutu", "666999", "male");
            int result = userMapper.updateUserWithSet(user);

            System.out.println("影响的行数：" + result);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p4ch05a-125页a：使用foreach标签遍历List集合
     */
    @Test
    public void testQueryUserWithForeachByList() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            List<Integer> userIdList = new ArrayList<>();
            userIdList.add(1);
            userIdList.add(2);
            userIdList.add(3);

            List<User> userList = userMapper.queryUserWithForeachByList(userIdList);
            System.out.println(userList);
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p4ch05a-126页a：使用foreach标签遍历数组
     */
    @Test
    public void testQueryUserWithForeachByArray() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            int[] userIdArray = {1,2,3};

            List<User> userList = userMapper.queryUserWithForeachByArray(userIdArray);
            System.out.println(userList);
        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p4ch05a-126页b：使用foreach标签遍历Map
     */
    @Test
    public void testQueryUserWithForeachByMap() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            Map<String, Object> userMap = new HashMap<>();
            userMap.put("gender", "female");

            List<Integer> userIdList = new ArrayList<>();
            userIdList.add(1);
            userIdList.add(2);
            userIdList.add(3);
            userMap.put("userIdList",userIdList);

            List<User> userList = userMapper.queryUserWithForeachByMap(userMap);
            System.out.println(userList);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

}
